package com.ay.weixin.autoreply.event.handle;

import java.io.IOException;
import java.sql.SQLException;

import org.lizar.controller.loader.EventLoader;
import org.lizar.ioc.Cell;
import org.lizar.ioc.Container;
import org.lizar.json.JObject;
import org.lizar.util.StringHelper;

import com.ay.weixin.autoreply.db.IbatisAutoReplyDao;
import com.ay.weixin.autoreply.db.IbatisClickEventDao;
import com.ay.weixin.autoreply.domain.ClickEvent;
import com.ay.weixin.autoreply.domain.Message;

public class HandleClickEvent implements Cell {
	
	private HandleMsg handl_msg;
	private IbatisAutoReplyDao auto_msg_dao;
	private IbatisClickEventDao click_event_dao;
	private HandleDuoKefu handle_duokefu;
	private HandleSubscribe handle_subscribe;
	
	
	
	@Override
	public void init_property() throws Exception {
		auto_msg_dao=Container.get(IbatisAutoReplyDao.class);
		click_event_dao=Container.get(IbatisClickEventDao.class);
		handle_duokefu=Container.get(HandleDuoKefu.class);
		handle_subscribe=Container.get(HandleSubscribe.class);
		handl_msg=Container.get(HandleMsg.class);
		
		
		
		
	}

	public void handle(long wxproject_id,JObject input,EventLoader el) throws IOException{
		
		String event_key=input.getStr("event_key");
		ClickEvent ce=null;
		try {
			ce=click_event_dao.getClickEvent(wxproject_id,event_key);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		if(ce==null){
			
			return;
		}
		if(StringHelper.equals(ce.getEvent_type(), "type_transfer_to_duokefu")){
			handle_duokefu.handle(el, input);
		}else if(StringHelper.equals(ce.getEvent_type(), "type_subscribe_msg")){
			handle_subscribe.send_subscribe_message(wxproject_id,input, el);
		}else if(StringHelper.equals(ce.getEvent_type(), "type_msg")){
			Message msg=null;
			try {
				msg=auto_msg_dao.getCLICKMessage(wxproject_id,"type_msg",input.getStr("event_key"));
			} catch (SQLException e) {
				e.printStackTrace();
				return;
			}
			if(msg!=null){
				handl_msg.reply(wxproject_id,msg, el, input);
				
			}
		}
		
	}
	
	

	
}
